package com.unnamed.b.atv.sample.provider;

import ohos.agp.components.LayoutScatter;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import com.unnamed.b.atv.sample.ResourceTable;
import ohos.app.Context;
import java.util.List;

/**
 * SimpleProvider
 *
 * @author Administrator
 * @Date 2021/5/26
 */
public class SimpleProvider extends BaseItemProvider {
    private List<String> list;
    private Context slice;

    public SimpleProvider(List<String> list, Context slice) {
        this.list = list;
        this.slice = slice;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        if (list != null && position > 0 && position < list.size()) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_list_item, null, false);
        } else {
            cpt = convertComponent;
        }
        Text name = (Text) cpt.findComponentById(ResourceTable.Id_text);
        name.setText(list.get(position));
        return cpt;
    }

}
